/*
 * @Author: ZhaoZhiqi
 * @Date: 2022-07-19 15:56:30
 * @LastEditors: ZhaoZhiqi
 * @LastEditTime: 2022-12-13 15:17:18
 * @Description: 路由配置
 * @FilePath: \tuanfeng-admin-front\src\router\index.ts
 */
import { createRouter, createWebHashHistory } from 'vue-router';
// import { Tickets, Message, ChatDotRound, OfficeBuilding, Briefcase, Document, TakeawayBox, Phone } from "@element-plus/icons-vue";
import { useRoleStore } from "../store/role.js";
import NotFound from "../pages/NotFound.vue";
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
NProgress.configure({ showSpinner: true }) // NProgress Configuration

let routes = [{
    path: '/',
    name: 'Root',
    component: () => import('../layout/Layout.vue'),
    children: [
        // {
        //     path: '/window',
        //     name: 'Window',
        //     meta: {
        //         title: '窗口电话',
        //         icon: Phone
        //     },
        //     component: () => import('../pages/Window/index.vue'),
        // },
        {
            path: "/:pathMatch(.*)*",
            name: "NotFound",
            meta: {},
            component: NotFound,
        }
    ]
}, {
    path: '/log',
    component: () => import('../pages/Log.vue'),
}]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

router.beforeEach(async (to, from, next) => {
    NProgress.start()
    const roleStore = useRoleStore()

    if (to.fullPath === '/log' || roleStore.routes) {
        next();
    } else {
        let res = await roleStore.getMenus();
        if (res) {
            router.addRoute({
                path: '/',
                name: 'Root',
                component: () => import('../layout/Layout.vue'),
                redirect: res.length ? res[0] : '/',
                children: [...res, {
                    path: "/:pathMatch(.*)*",
                    name: "NotFound",
                    meta: {},
                    component: NotFound,
                }]
            });

            next(to.fullPath);
        } else {
            next('/log');
        }
    }
})

router.afterEach(() => {
    NProgress.done()
})

export default router
